<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
class Card extends Model{
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';
	// 自动完成
	protected $auto = [];
	protected $insert = ['card_addtime'];
	protected $update = ['card_usetime'];	
	protected function setcardaddtimeAttr(){
        return time();
    }
	protected function setcardusetimeAttr(){
        return time();
    }
	public $fieldlist = [1,5,10,50,100,500,1000];
	public function user(){
		return $this->hasOne('user','user_id','card_uid');
    }	
	//卡密充值
	public function recharge($card_number, $card_uid){
		$where = array();
		$where['card_number'] = array('eq',$card_number);
		$where['card_status'] = array('eq',0);
		$info = $this->field('card_id,card_score')->where($where)->find();
		if(empty($info)){
			$this->error = "卡密错误或已充值！";
			return false;
		}
		//更新用户积分
		model('Score')->user_score($card_uid,8,$info['card_score'],0,0);
		//更新卡密状态
		$this->save(['card_status' =>1,'card_uid' => $card_uid],['card_id' => $info['card_id']]);
		$result['data']= $info['card_score'];
		$result['id']=$info['card_id'];
		//正常返回卡密ID
		return $result;
	}
	
}